package com.lagou.base;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

public class BaseServlet extends HttpServlet {

    /*
     * doGET方法作为一个调度器 .根据请求功能的不同,调用对应的方法
     *      规定必须传递一个参数
     *           methodName=功能名
     * */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.获取参数 要访问的方法名
        //String methodName = req.getParameter("methodName");
        String methodName = null;

        //1获取post请求的Content-type类型
        String contentType = req.getHeader("Content-Type");

        //2判断传递的数据是不是JSON
        if("application/json;charset=utf-8".equals(contentType)){
            //是json格式，调用getpostjson
            String postJSON = getPostJSON(req);
            
            //将json格式的字符串转化为map
            Map<String,Object> map = JSON.parseObject(postJSON, Map.class);

            //从map集合中获取methodName
            methodName = (String) map.get("methodName");
            //将获取到的数据，保存到request域对象中
            req.setAttribute("map",map);
        }else{
            methodName = req.getParameter("methodName");
        }
        //2.判断 执行对应的方法
        if(methodName != null){
            //通过反射优化代码 提升代码的可维护性

            try {
                //1.获取字节码文件对象
                Class c = this.getClass();

                //2.根据传入的方法名,获取对应的方法对象  findByName
                Method method = c.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);

                //3.调用method对象的 invoke方法,执行对应的功能
                method.invoke(this,req,resp);

            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("请求的功能不存在!!");
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    //post请求格式为json，使用该方法获取
    public String getPostJSON(HttpServletRequest request) {
        try {
            //1从request中获取缓冲输入流
            BufferedReader reader = request.getReader();
            //2创建StringBuffer 保存读取出的数据
            StringBuffer sb = new StringBuffer();
            //3循环读取
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            //4返回结果
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
